*************************************************
*** SUMMARY STATS FROM HOSPITAL-LEVEL FILE    ***
*** Last edited: 3/1/21         		    ***
*************************************************

log using "${CodePath}/log/summarize_summarystats_$S_DATE.log", text replace

cap mkdir "${OutputPath}/RACRegion/jackknife_audit_in2011/within100m"
cap mkdir "${OutputPath}/RACRegion/jackknife_audit_in2011/within100m/SummaryStats/"
*--------------------------------------------------
* Summary stats from hospital-level file
*--------------------------------------------------


use "${DataPath}/derived/hospyear_0716_jk.dta", clear
keep if !missing(RACregion_audit_in2011) //restrict to acute hospitals
// define sample of hospitals within 100 miles
preserve
	use "${DataPath}/within100m/pnlist_groups_100m.dta", clear
	duplicates drop pn, force
	keep pn hospcomp_group_100 n_hospcomp_ingroup_100
	gen sample_hosp_100mradius = 1
	tempfile temp
	save `temp'
restore

merge m:1 pn using `temp', keep(1 3) nogen
replace sample_hosp_100mradius = 0 if missing(sample_hosp_100mradius)

// binscatter of first stage

	reg audit_in2011 jk_state_audit_in2011 if fyear == 2011 & sample_hosp_100mradius == 1 & n_hospcomp_ingroup_100 != 1, noconstant
	qui reg audit_in2011 jk_state_audit_in2011 if fyear == 2011 & sample_hosp_100mradius == 1 & n_hospcomp_ingroup_100 != 1, noconstant
	local coef=string(_b[jk_state_audit_in2011],"%9.2f")
	local se=string(_se[jk_state_audit_in2011],"%9.2f")
	local r2=string(e(r2),"%9.2f")
	local N = string(e(N), "%9.0f")

	binscatter audit_in2011 jk_state_audit_in2011 if fyear == 2011 & sample_hosp_100mradius == 1 & n_hospcomp_ingroup_100 != 1, ///
		xtitle("2011 leave-one-out audit rate (%)") ytitle("2011 audit rate (%)") ///
		note(`"Regression:"' `"Coef = `coef' "' `"(`se')"' `"R2 = `r2' "' `"N = `N' "', ring(0) pos(5) size(small))

	graph export "${OutputPath}/RACRegion/jackknife_audit_in2011/within100m/SummaryStats/audit_jk_state_audit_in2011.pdf", replace 

	binscatter audit_in2011 jk_state_audit_in2011 if fyear == 2011 & sample_hosp_100mradius == 1 & n_hospcomp_ingroup_100 != 1, ///
		xtitle("2011 leave-one-out audit rate (%)") ytitle("2011 audit rate (%)")

	graph export "${OutputPath}/RACRegion/jackknife_audit_in2011/within100m/SummaryStats/audit_jk_state_audit_in2011_nocoeff.pdf", replace 

	binscatter audit_in2011 jk_state_audit_in2011 if fyear == 2011 & sample_hosp_100mradius == 1 & n_hospcomp_ingroup_100 != 1, ///
		xtitle("2011 leave-one-out audit rate (%)", size(large)) ytitle("2011 audit rate (%)", size(large)) ///
		xlabel(,labsize(large)) ylabel(,labsize(large) angle(horizontal)) ///
		note(`"Regression:"' `"Coef = `coef' "' `"(`se')"' `"R2 = `r2' "' `"N = `N' "', ring(0) pos(5) size(medium))

	graph export "${OutputPath}/RACRegion/jackknife_audit_in2011/within100m/SummaryStats/audit_jk_state_audit_in2011_large.pdf", replace


// corr b/w 2011 audit rate and denial rate by year

	qui reg audit_in2011 demandrate_in2011 if fyear == 2011 & sample_total == 1
	local coef=string(_b[demandrate_in2011],"%9.3f")
	local se=string(_se[demandrate_in2011],"%9.3f")
	local r2=string(e(r2),"%9.3f")
	local N = string(e(N), "%9.0f")

	binscatter audit_in2011 demandrate_in2011 if fyear == 2011 & sample_total == 1, ///
		ytitle("2011 audit rate (%)", size(large)) xtitle("2011 denial rate (%)", size(large)) ///
		note(`"Regression:"' `"Coef = `coef' "' `"(`se')"' `"R2 = `r2' "' `"N = `N' "', ring(0) pos(5) size(medlarge))

	graph export "${OutputPath}/Summary Statistics/audit_in2011_demandrate_in2011.pdf", replace
	

// coefplot of relationship between hospital characteristics and audit rate, and likelihood of having audit at all
gen no_audit_in2011 = audit_in2011 == 0
gen no_audit_in2012 = audit_in2012 == 0

reg no_audit_in2011 beds i.RACregion if sample_total == 1 & fyear == 2011
reg no_audit_in2011 i.hospprofittype i.RACregion if sample_total == 1 & fyear == 2011
reg no_audit_in2011 i.urban i.RACregion if sample_total == 1 & fyear == 2011
reg no_audit_in2011 app_live_136 i.RACregion if sample_total == 1 & fyear == 2010

reg no_audit_in2012 no_audit_in2011 i.RACregion if fyear == 2011

cap drop std_*
center beds sh_claims_02 mcr_pct, standardize prefix(std_)

label var std_beds "beds (std.)"
label var std_sh_claims_02 "short stay share (std.)"
label var std_mcr_pct "Medicare days share (std.)"
label var forprofit "for-profit"
label var nonprofit "non-profit"
label var independent "non-chain"

label define hospprofittype_l 1 "non-profit" 2 "for-profit" 3 "gov't", modify
label values hospprofittype hospprofittype_l

label define independent_type_l 0 "chain" 1 "non-chain" , modify
label values independent independent_type_l

label var app_live_136 "med. necessity app installed"


reg audit_in2011 std_beds  std_sh_claims_02 std_mcr_pct app_live_136 i.independent i.hospprofittype i.urban i.RACregion if sample_total == 1 & fyear == 2011
eststo m2, title("RAC FE")

coefplot (m2), ///
	omitted baselevels ///
	headings(0.independent = "{bf:Hospital Chain/Non-Chain}" ///
	1.hospprofittype = "{bf:Hospital Profit Type}" ///
	1.RACregion = "{bf:RAC Region}" ///
	0.urban = "{bf:Hospital Location}") ///
	xline(0, lcolor(black) lpattern(dash)) ///
	drop(_cons) graphregion(color(white)) ///
	xtitle("Coefficient on 2011 audit rate")
graph export "${OutputPath}/Summary Statistics/hospcharcoeffs_audit_in2011.pdf", replace

reg no_audit_in2011 std_beds  std_sh_claims_02 std_mcr_pct app_live_136 i.independent i.hospprofittype independent i.urban i.RACregion if sample_total == 1 & fyear == 2011
eststo m4
coefplot (m4), ///
	omitted baselevels ///
	headings(0.independent = "{bf:Hospital Chain/Non-Chain}" ///
	1.hospprofittype = "{bf:Hospital Profit Type}" ///
	1.RACregion = "{bf:RAC Region}" ///
	0.urban = "{bf:Hospital Location}") ///
	xline(0, lcolor(black) lpattern(dash)) ///
	drop(_cons) graphregion(color(white)) ///
	xtitle("Coefficient on having no audits in 2011")
graph export "${OutputPath}/Summary Statistics/hospcharcoeffs_no_audit_in2011.pdf", replace

gen tot_pmt_amt_mill = tot_pmt_amt/1000000

gen n_claims_1000 = n_claims/1000
gen real_gcost_admin_net_mill = real_gcost_admin_net/1000000
gen real_tot_net_mill = real_tot_net/1000000
// get counts of how many hospitals are in sample
gen sample_border = sample_hosp_100mradius == 1 & n_hospcomp_ingroup_100 != 1
tab RACregion if sample_total == 1 & sample_border == 1 & fyear == 2010

gen n_sample_border_temp = sample_border == 1 & fyear == 2011
bys RACregion: egen n_sample_border = sum(n_sample_border_temp)

// get counts of how many hospitals are within 100 miles of border
gen dist_to_border_100m_temp = sample_hosp_100mradius == 1 & fyear == 2011
bys RACregion: egen dist_to_border_100m = sum(dist_to_border_100m_temp)



label var audit_in2011 "2011 audit rate"
label var beds "beds"
label var urban "share urban"
label var share_gcost_admin_net "admin share"
label var real_gcost_admin_net_mill "net admin costs (million \$)"
label var real_tot_net_mill "total cost (million \$)"
label var app_contracted_136 "share medical necessity application installed"
label var n_claims_1000 "admissions (thousands)"
label var mean_los "mean Medicare length of stay"
label var mean_pmt_amt "mean Medicare payment (\$)"
label var tot_pmt_amt_mill "Medicare payments (million \$)"
label var wageindex "wage index"
label var nonprofit "share non-profit"
label var forprofit "share for-profit"
label var govt "share government"
label var independent "share independent"
label var dist_to_border_100m "n within 100 miles of RAC border"
label var n_sample_border "n in border hospital sample"
label var n_claims_02 "n los = 0-2 stays"
rename pred_0709_aud2011 sh_pred_0709_aud2011
label var sh_pred_0709_aud2011 "predicted audit rate"


gen RACregion_A = RACregion == 1
gen RACregion_B = RACregion == 2
gen RACregion_C = RACregion == 3
gen RACregion_D = RACregion == 4

label var RACregion_A "Region A"
label var RACregion_B "Region B"
label var RACregion_C "Region C"
label var RACregion_D "Region D"


eststo clear
estpost summarize audit_in2011 RACregion_A RACregion_B RACregion_C RACregion_D beds urban nonprofit forprofit govt real_tot_net_mill real_gcost_admin_net_mill n_claims mean_pmt_amt tot_pmt_amt_mill sh_claims_02 sh_n_claims_top20error n_sample_border if fyear == 2010 & sample_total == 1, listwise
eststo overall
estpost summarize audit_in2011 RACregion_A RACregion_B RACregion_C RACregion_D beds urban nonprofit forprofit govt real_tot_net_mill real_gcost_admin_net_mill n_claims mean_pmt_amt tot_pmt_amt_mill sh_claims_02 sh_n_claims_top20error n_sample_border if fyear == 2010 & sample_hosp_100mradius == 1 & n_hospcomp_ingroup_100 != 1, listwise
eststo border
esttab using "${OutputPath}/Summary Statistics/hosp_audit_in2011_overall_100mradius_means.tex", replace ///
	cells("mean(fmt(2))" "sd(fmt(2) par)") mtitles("Overall" "Border") sfmt(%9.1f) label


// by RAC region
// above vs. below median audit, overall and border hospitals
*eststo clear
estpost summarize audit_in2011 beds urban nonprofit forprofit govt real_tot_net_mill real_gcost_admin_net_mill n_claims mean_pmt_amt tot_pmt_amt_mill sh_claims_02  sh_n_claims_top20error n_sample_border if RACregion == 1 & fyear == 2010 & sample_total == 1, listwise
eststo RAC_A
estpost summarize audit_in2011 beds urban nonprofit forprofit govt real_tot_net_mill real_gcost_admin_net_mill n_claims mean_pmt_amt tot_pmt_amt_mill sh_claims_02  sh_n_claims_top20error n_sample_border if RACregion == 2 & fyear == 2010 & sample_total == 1, listwise
eststo RAC_B
estpost summarize audit_in2011 beds urban nonprofit forprofit govt real_tot_net_mill real_gcost_admin_net_mill n_claims mean_pmt_amt tot_pmt_amt_mill sh_claims_02  sh_n_claims_top20error n_sample_border if RACregion == 3 & fyear == 2010 & sample_total == 1, listwise
eststo RAC_C
estpost summarize audit_in2011 beds urban nonprofit forprofit govt real_tot_net_mill real_gcost_admin_net_mill n_claims mean_pmt_amt tot_pmt_amt_mill sh_claims_02  sh_n_claims_top20error n_sample_border if RACregion == 4 & fyear == 2010 & sample_total == 1, listwise
eststo RAC_D
esttab RAC_A RAC_B RAC_C RAC_D using "${OutputPath}/Summary Statistics/hosp_audit_in2011_byRACregion_short.tex", replace ///
	cells("mean(fmt(2))" "sd(fmt(2) par)") mtitles("A" "B" "C" "D") sfmt(%9.1f) label

// all combined

esttab using "${OutputPath}//Summary Statistics/hosp_audit_in2011_byRACregion_overall_100mradius_short.tex", replace ///
	cells("mean(fmt(2))" "sd(fmt(2) par)") mtitles("Overall" "Border" "A" "B" "C" "D") sfmt(%9.1f) label

// do predicted audit rate separately bc it cuts down sample size
eststo clear
estpost summarize sh_pred_0709_aud2011 if fyear == 2010 & sample_total == 1, listwise
eststo overall
estpost summarize sh_pred_0709_aud2011 if fyear == 2010 & sample_hosp_100mradius == 1 & n_hospcomp_ingroup_100 != 1, listwise
eststo border
estpost summarize sh_pred_0709_aud2011 if fyear == 2010 & sample_total == 1 & RACregion == 1, listwise
eststo RAC_A
estpost summarize sh_pred_0709_aud2011 if fyear == 2010 & sample_total == 1 & RACregion == 2, listwise
eststo RAC_B
estpost summarize sh_pred_0709_aud2011 if fyear == 2010 & sample_total == 1 & RACregion == 3, listwise
eststo RAC_C
estpost summarize sh_pred_0709_aud2011 if fyear == 2010 & sample_total == 1 & RACregion == 4, listwise
eststo RAC_D

esttab using "${OutputPath}//Summary Statistics/pred_auditrate_byRACregion_overall_100mradius_short.tex", replace ///
	cells("mean(fmt(2))" "sd(fmt(2) par)") mtitles("Overall" "Border" "A" "B" "C" "D") sfmt(%9.1f) label


// n audit, n demands, and amount demanded by year
use "${DataPath}/Audit Data/byclaimtype_1_withCCN.dta", clear
gen audit_year = year(Date_InitSelect)
gen audit_mon = month(Date_InitSelect)
gen claim_year = year(Date_SvcEnd)
gen claim_month = year(Date_SvcStart)

gen audit_fyear = audit_year
replace audit_fyear = audit_fyear + 1 if audit_mon >= 10

gen claim_fyear = claim_year
replace claim_fyear = claim_fyear + 1 if claim_mon >= 10

gen DemandAmt2 = DemandAmt
replace DemandAmt2 = 0 if missing(DemandAmt)

gen DemandAmt_abs = abs(DemandAmt)

gen 	demanded = 0
replace demanded = 1 if !missing(DemandAmt)

gen audit = 1

gen demanded_over = 0
replace demanded_over = 1 if !missing(DemandAmt) & DemandAmt > 0

// per-provider
gcollapse (count) n_audits = DemandAmt2 (sum) amtaudited = Orig_ClaimAmt amtdemanded = DemandAmt2 amtdemanded_abs = DemandAmt_abs n_demands = demanded n_demands_over = demanded_over, by(audit_fyear provider)
/* gen nonnum = real(provider) == .
drop if nonnum
destring provider, gen(pn) */
rename provider pn

// share of provider's 2010 revenue
preserve
	use "${DataPath}/derived/hospyear_0716_jk.dta", clear
	keep if !missing(RACregion_audit_in2011) //restrict to acute hospitals
	keep if fyear == 2008
	keep pn tot_pmt_amt
	tempfile temp
	save `temp'
restore
merge m:1 pn using `temp', keep(1 3)

gen share_demanded_2008 = amtdemanded/tot_pmt_amt*100
gen share_audited_2008 = amtaudited/tot_pmt_amt*100

gcollapse (mean) share_demanded_2008 share_audited_2008 n_audits amtaudited amtdemanded amtdemanded_abs n_demands n_demands_over, by(audit_fyear)

gen amtaudited_million = amtaudited/1000000
gen amtdemanded_million= amtdemanded/1000000
twoway (connected amtaudited_million audit_fyear) /// 
	(connected amtdemanded_million audit_fyear, lpattern(dash)), ///
	legend(order(1 "Value of audited payments" 2 "Value of reclaimed payments") position(1) ring(0) rows(2) size(medlarge) region(lcolor(white))) ///
	graphregion(color(white)) ///
	xlabel(2009(1)2020, angle(45))  xtitle(Fiscal Year)  ///
	subtitle("million $ per hospital", position(11) justification(left) size(medlarge)) ylabel(,angle(horizontal)) ///
	xline(2010.75, lcolor(black) lpattern(dash))
graph export "${OutputPath}/Summary Statistics/byyearhosp_n_audits_n_demands_amtdemanded.pdf", replace

twoway (connected share_audited_2008 audit_fyear) /// 
	(connected share_demanded_2008 audit_fyear, lpattern(dash)), ///
	legend(order(1 "Value of audited payments" 2 "Value of reclaimed payments") position(1) ring(0) rows(2) size(medlarge) region(lcolor(white))) ///
	graphregion(color(white)) ///
	xlabel(2009(1)2020, angle(45))  xtitle(Fiscal Year)  ///
	subtitle("percent relative to 2008 Medicare revenue", position(11) justification(left) size(medlarge)) ylabel(,angle(horizontal)) ///
	xline(2010.75, lcolor(black) lpattern(dash))
graph export "${OutputPath}/Summary Statistics/byyearhosp_share_amtaudited_amtdemanded.pdf", replace



*--------------------------------------------------
* Summary stats from hospital-claim-type file
*--------------------------------------------------
// by LOS bin, overall
use "${DataPath}/claims/auditrates_byLOSbin_0716.dta", clear
forval year = 2010/2015{
	preserve
		// across all hospitals, by year of audits
			gcollapse (sum) totaudclaims_`year' totaudits_`year' totdemands_`year', by(LOSbin)
			gen auditrate_`year' = totaudits_`year'/totaudclaims_`year'*100
			gen demandrate_`year' = totdemands_`year'/totaudclaims_`year'*100

			gen LOSbin2 = _n
			replace LOSbin2 = 22 if LOSbin == 2150
			replace LOSbin2 = 23 if LOSbin == 51100
			replace LOSbin2 = 24 if LOSbin == 101
			sort LOSbin2
			label define LOSbin_l 1 "0" 2 "1" 3 "2" 4 "3" 5 "4" 6 "5" 7 "6" 8 "7" 9 "8" 10 "9" 11 "10" 12 "11" 13 "12" 14 "13" 15 "14" 16 "15" 17 "16" 18 "17" 19 "18" 20 "19" 21 "20" 22 "21-50" 23 "51-100" 24 "101+"
			label values LOSbin2 LOSbin_l
			
			graph bar auditrate_`year' demandrate_`year', over(LOSbin2, lab(angle(45))) bar(1, fcolor(erose) lcolor(maroon)) bar(2, fcolor(dknavy) lcolor(ebg)) ///
				graphregion(color(white)) ///
				b1title("Length of stay") ///
				ylab(,angle(0)) ///
				legend(order(1 "`year' audit rate" 2 "`year' denial rate") position(2) ring(0))

			graph export "${OutputPath}/Summary Statistics/byLOSbin_auditdemandrate_`year'.pdf", replace

			graph bar totaudits_`year' totdemands_`year', over(LOSbin2, lab(angle(45))) bar(1, fcolor(erose) lcolor(maroon)) bar(2, fcolor(dknavy) lcolor(ebg)) ///
				graphregion(color(white)) ///
				b1title("Length of stay") ///
				ylab(,angle(0)) ///
				legend(order(1 "`year' audits" 2 "`year' denials") position(2) ring(0))

			graph export "${OutputPath}/Summary Statistics/byLOSbin_auditdemands_`year'.pdf", replace
	restore

}
gen shortstay = LOSbin <= 2
gcollapse (sum) totaudclaims_2011 totaudits_2011 totdemands_2011, by(shortstay)

egen sum_totaudclaims_2011 = sum(totaudclaims_2011)
egen sum_totaudits_2011 = sum(totaudits_2011)
gen sh_totaudclaims_2011 = totaudclaims_2011/sum_totaudclaims_2011
gen sh_totaudits_2011 = totaudits_2011/sum_totaudits_2011

// audit rate by top error DRGs
	use "${DataPath}/claims/auditrates_by_top20msdrg_0716.dta", clear
	forval year = 2010/2015{
		preserve
			// across all hospitals, by year of audits
				gcollapse (sum) totaudclaims_`year' totaudits_`year' totdemands_`year', by(drg_toperror)

				gen auditrate_`year' = totaudits_`year'/totaudclaims_`year'*100
				gen demandrate_`year' = totdemands_`year'/totaudclaims_`year'*100

				label define drg_l 1 "major joint" 2 "cardiac pacemaker" 3 "drug-eluting stent" 4 "sepsis" ///
					5 "chest pain" 6 "GI hem." 7 "bowel" 8 "resp. infect." 9 "esophagitis" ///
					10 "kidney" 11 "nutritional" 12 "renal failure" 13 "syncope" 14 "heart failure" 15 "cardiac arrhythmia" ///
					16 "pneumonia" 17 "AMI" 18 "COPD" 19 "hip and femur" 20 "intracranial hem." 100 "other"
				label values drg_toperror drg_l
				
				graph bar auditrate_`year' demandrate_`year', over(drg_toperror, lab(angle(90))) bar(1, fcolor(erose) lcolor(maroon)) bar(2, fcolor(dknavy) lcolor(ebg)) ///
					graphregion(color(white)) ///
					b1title("Base DRG (highest to lowest errors)") ///
					ylab(,angle(0)) ///
					legend(order(1 "`year' audit rate" 2 "`year' denial rate") position(2) ring(0))

				graph export "${OutputPath}/Summary Statistics/byDRGerror_auditdemandrate_`year'.pdf", replace

				graph bar totaudits_`year' totdemands_`year', over(drg_toperror, lab(angle(90))) bar(1, fcolor(erose) lcolor(maroon)) bar(2, fcolor(dknavy) lcolor(ebg)) ///
					graphregion(color(white)) ///
					b1title("Base DRG (highest to lowest errors)") ///
					ylab(,angle(0)) ///
					legend(order(1 "`year' audits" 2 "`year' denials") position(2) ring(0))

				graph export "${OutputPath}/Summary Statistics/byDRGerror_auditdemands_`year'.pdf", replace
		restore

	}

	sum totaudits_2011 if drg_toperror <= 20
	local top20_audits = r(sum)
	sum totaudclaims_2011 if drg_toperror <= 20
	local top20_claims = r(sum)

	local auditrate_2011_top20 = `top20_audits'/`top20_claims'
	di "`auditrate_2011_top20'"


*--------------------------------------------------
* Relationship between having medical necessity checking app and auditing
*--------------------------------------------------
use "${DataPath}/derived/hospyear_0716_jk.dta", clear
keep if !missing(RACregion_audit_in2011) //restrict to acute hospitals

// define sample of hospitals within 100 miles
preserve
	use "${DataPath}/within100m/pnlist_groups_100m.dta", clear
	duplicates drop pn, force
	keep pn hospcomp_group_100 n_hospcomp_ingroup_100
	gen sample_hosp_100mradius = 1
	tempfile temp
	save `temp'
restore

merge m:1 pn using `temp', keep(1 3) nogen
replace sample_hosp_100mradius = 0 if missing(sample_hosp_100mradius)


	local plotvars "_year_ _b_ _min95_ _max95_ _b_RAC_ _b_state_ _min95_RAC_ _max95_RAC_ _min95_state_ _max95_state_ _year_RAC_ _year_state_"
	foreach var of local plotvars{
		cap drop `var'
		gen `var' = .
	}

	forval year = 2010/2015{
		local i = `year' - 2009
		replace _year_ = `year' in `i'
		reg demandrate_in`year' app_live_136 if fyear == 2010
		replace _b_ = _b[app_live_136]					 in `i'
		replace _min95_ = _b_ - _se[app_live_136]*1.96   in `i'
		replace _max95_ = _b_ + _se[app_live_136]*1.96   in `i'


		replace _year_RAC_ = `year' +0.1 in `i'
		reghdfe demandrate_in`year' app_live_136 if fyear == 2010, absorb(RACregion) 
		replace _b_RAC_ = _b[app_live_136]					 in `i'
		replace _min95_RAC_ = _b_RAC_ - _se[app_live_136]*1.96   in `i'
		replace _max95_RAC_ = _b_RAC_ + _se[app_live_136]*1.96   in `i'

		reghdfe demandrate_in`year' app_live_136 if fyear == 2010, absorb(state)
		replace _year_state_ = `year' + 0.2 in `i'
		replace _b_state_ = _b[app_live_136]   in `i'
		replace _min95_state_ = _b_state_ - _se[app_live_136]*1.96   in `i'
		replace _max95_state_ = _b_state_ + _se[app_live_136]*1.96   in `i'
		
	}

	twoway (scatter _b_ _year_, color(dknavy)) (rcap _min95_ _max95_ _year_, color(dknavy%50)) ///
			(scatter _b_RAC_ _year_RAC_, color(maroon)) (rcap _min95_RAC_ _max95_RAC_ _year_RAC_, color(maroon%50)) ///
			(scatter _b_state_ _year_state_, color(forest_green)) (rcap _min95_state_ _max95_state_ _year_state_, color(forest_green%50)), ///
			legend(order(1  "No FE" 3 "With RAC FE" 5 "With state FE") col(3)) graphregion(color(white)) xtitle("Audit Year") ytitle("Coefficient on 2010 MN App") ///
			yline(0, lcolor(black) lpattern(dash))

	graph export "${OutputPath}/Summary Statistics/corr_app_live_136_demandrates.pdf", replace

// hospitals with higher demands more likely to install
	gen DemandAmt_3y_1e6 = DemandAmt_3y/1000000
	sum DemandAmt_3y_1e6 if fyear == 2011, detail
	local p99 = r(p99)

	reg install_app_136_1115 DemandAmt_3y_1e6 if fyear == 2011 & DemandAmt_3y_1e6 < `p99'& sample_total, absorb(RACregion)

	gen reg_sample = r(sample)

	binscatter install_app_136_1115 DemandAmt_3y_1e6 if reg_sample, ///
		absorb(RACregion) ///
		graphregion(color(white)) ///
		xtitle("Amount demanded, 2011 (millions)", size(large)) ytitle("") ///
		subtitle("Share install MN software, 2011-2015", position(11) justification(left) size(large)) ylabel(,angle(horizontal)) ///
		xlabel(,labsize(large)) ylabel(,labsize(large)) ///
		caption("Absorbing RAC FE.")

	graph export "${OutputPath}/Summary Statistics/sh_installMNapp_DemandAmt2011.pdf", replace

// hospitals that install have larger reductions in emergent stays
	bys pn (fyear): gen d2_emergent 	= drg_top20_emergent - drg_top20_emergent[_n-2]
	bys pn (fyear): gen d2_nonemergent = drg_top20_nonemergent - drg_top20_nonemergent[_n-2]

	reg install_app_136_1115 d2_emergent if fyear == 2012 & sample_total, absorb(RACregion)

	binscatter install_app_136_1115 d2_emergent if sample_total == 1 & fyear == 2012, ///
		absorb(RACregion) ///
		graphregion(color(white)) ///
		xtitle("Change in high-error emergent stays", size(large)) ytitle("") ///
		subtitle("Share install MN software, 2011-2015", position(11) justification(left) size(large)) ylabel(,angle(horizontal)) ///
		xlabel(,labsize(large)) ylabel(,labsize(large)) ///
		caption("Absorbing RAC FE.")
	graph export "${OutputPath}/Summary Statistics/sh_installMNapp_d2_top20emergent.pdf", replace

	reg install_app_136_1115 d2_nonemergent if fyear == 2012 & sample_total, absorb(RACregion)

	binscatter install_app_136_1115 d2_nonemergent if sample_total == 1 & fyear == 2012, ///
		absorb(RACregion) ///
		graphregion(color(white)) ///
		xtitle("Change in high-error non-emergent stays", size(large)) ytitle("") ///
		subtitle("Share install MN software, 2011-2015", position(11) justification(left) size(large)) ylabel(,angle(horizontal)) ///
		xlabel(,labsize(large)) ylabel(,labsize(large)) ///
		caption("Absorbing RAC FE.")

	graph export "${OutputPath}/Summary Statistics/sh_installMNapp_d2_top20nonemergent.pdf", replace

log close

